/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cip.esymons.eventos.adapaters;

import java.awt.Desktop;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JRRtfExporter;
import net.sf.jasperreports.engine.export.JRXlsExporter;
import net.sf.jasperreports.engine.export.JRXlsExporterParameter;
import org.cip.esymons.eventos.be.Evento;

/**
 *
 * @author OscarA
 */
public class ReporteAsistentesController {

    //BAse de datos
    Connection conn;
    PreparedStatement prepared;
    //evento seleccionado parametro
    Evento eventoSeleccionado;
    ArrayList<ReporteAsistentes> listaReportes = new ArrayList<ReporteAsistentes>();
//    boolean val = false;
    int cantidad;

    public ReporteAsistentesController() {
        conectar();
        reporte_Participantes();
    }

    public void conectar() {
        String server = "localhost";//10.1.2.53
        String password = "admin";//3sym0ns
        try {
            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("Driver JDBC instalado");
            conn = DriverManager.getConnection(
                    "jdbc:mysql://"+server+":3306/esymons", "root", password);
        } catch (Exception ex) {
            Logger.getLogger(ReporteAsistentesController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    //Get y Set

    public Evento getEventoSeleccionado() {
        return eventoSeleccionado;
    }

    public void setEventoSeleccionado(Evento eventoSeleccionado) {
        this.eventoSeleccionado = eventoSeleccionado;
    }

    public int getCantidad() {
        return cantidad;
    }

    public void setCantidad(int cantidad) {
        this.cantidad = cantidad;
    }

    public ArrayList<ReporteAsistentes> getListaReportes() {
        return listaReportes;
    }

    public void setListaReportes(ArrayList<ReporteAsistentes> listaReportes) {
        this.listaReportes = listaReportes;
    }

    //procedimientos almacenados
    public List<ReporteAsistentes> reporteParticipantes(boolean val2) {
        listaReportes = new ArrayList<ReporteAsistentes>();
        listaReportes.clear();
        try {
            String sql = "call sp_mostrar_participante_inscritos(?)";
            prepared = conn.prepareStatement(sql);
            if (val2) {
                prepared.setInt(1, eventoSeleccionado.getEveCodigo());
            } else {
                prepared.setInt(1, 1);
            }
            ResultSet result = prepared.executeQuery();
            cantidad=0;
            while (result.next()) {
                ReporteAsistentes rep = new ReporteAsistentes();
                rep.setNroCIP(result.getString(1));
                rep.setParticipante(result.getString(2));
                rep.setEspecialidad(result.getString(4));
                rep.setCapitulo(result.getString(5));
                cantidad = result.getInt(6);
                listaReportes.add(rep);
            }
        } catch (Exception ex) {
            listaReportes = new ArrayList<ReporteAsistentes>();
              cantidad=0;
        }
        return listaReportes;
    }

    public String ReporteInscritosPDF() {
        try {
            HashMap p = new HashMap();
            p.put("evento", eventoSeleccionado.getEveCodigo());

            File entrada = new File("C:\\ReporteJaspert\\ListaInscritosEventos.jasper");
            JasperPrint print = JasperFillManager.fillReport(new FileInputStream(entrada), p, conn);
            OutputStream output = new FileOutputStream(new File("C:\\Reporte\\eve_reporte_Inscritos.pdf"));
            JasperExportManager.exportReportToPdfStream(print, output);
            output.close();
            //abrir cual tipo de documento
            File path = new File("C:\\Reporte\\eve_reporte_Inscritos.pdf");
            Desktop.getDesktop().open(path);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public String ReporteInscritosExcel() {
        try {
            HashMap p = new HashMap();
            p.put("evento", eventoSeleccionado.getEveCodigo());

            File entrada = new File("C:\\ReporteJaspert\\ListaInscritosEventos.jasper");
            JasperPrint print = JasperFillManager.fillReport(new FileInputStream(entrada), p, conn);
            //Exportar a Excel
            OutputStream ouputStream = new FileOutputStream(new File("C:\\Reporte\\eve_reporte_Inscritos.xls"));
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            JRXlsExporter exporterXLS = new JRXlsExporter();
            exporterXLS.setParameter(JRXlsExporterParameter.JASPER_PRINT, print);
            exporterXLS.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, byteArrayOutputStream);
            exporterXLS.exportReport();
            ouputStream.write(byteArrayOutputStream.toByteArray());
            ouputStream.flush();
            ouputStream.close();
            //abrir cual tipo de documento
            File path = new File("C:\\Reporte\\eve_reporte_Inscritos.xls");
            Desktop.getDesktop().open(path);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public String ReporteInscritosWord() {
        try {
            HashMap p = new HashMap();
            p.put("evento", eventoSeleccionado.getEveCodigo());

            File entrada = new File("C:\\ReporteJaspert\\ListaInscritosEventos.jasper");
            JasperPrint print = JasperFillManager.fillReport(new FileInputStream(entrada), p, conn);
            //Exportar a Excel
            OutputStream ouputStream = new FileOutputStream(new File("C:\\Reporte\\eve_reporte_Inscritos.rtf"));
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            JRRtfExporter exporter = new JRRtfExporter();
            exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
            exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);
            exporter.exportReport();
            ouputStream.write(byteArrayOutputStream.toByteArray());
            ouputStream.flush();
            ouputStream.close();
            //abrir cual tipo de documento
            File path = new File("C:\\Reporte\\eve_reporte_Inscritos.rtf");
            Desktop.getDesktop().open(path);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
    //Strings

    public String reporte_Participantes() {
        reporteParticipantes(false);
        return "";

    }

    public String recalcularCombo() {
       
        reporteParticipantes(true);
        return "";
    }
}
